<template>
	<view :style="{ minHeight: sys.windowHeight + 'px' }"
		:class="[$tm.vx.state().tmVuetify.black ? 'black' : 'grey text ']">
		<tm-menubars color="primary" title="个人中心" :shadow="0">
		</tm-menubars>
		<view>
			<tm-sheet :shadow="24" bg-color="orange">
				<tm-listitem :margin="[0,32]">
					<template>
						<tm-avatar :src="userInfo.avatar" :size="100"></tm-avatar>
						<text style="margin: 0 0 0 10rpx;">{{userInfo.nickname}}</text>
					</template>
				</tm-listitem>
				<tm-listitem :margin="[0,32]" title="我的账本" @click="openUrl('/pages/user/book')">
				</tm-listitem>
				<tm-listitem :margin="[0,32]" title="共享账本" @click="openUrl('/pages/user/share')">
				</tm-listitem>
				<tm-listitem :margin="[0,32]" title="我的预算" @click="openUrl('/pages/user/budget')">
				</tm-listitem>
				<tm-listitem :margin="[0,32]" title="开启/关闭通知" @click="openNotice">
				</tm-listitem>
				<tm-listitem :margin="[0,32]" title="关于" @click="abort">
				</tm-listitem>
				<tm-listitem :margin="[0,32]" title="退出登录" @click="exitLogin">
				</tm-listitem>
			</tm-sheet>
		</view>
		<tm-poup v-model="share" position="center" width="700">
			<tm-sheet>
				<view>记账泡是一个便捷记账服务程序</view>
			</tm-sheet>
		</tm-poup>
	</view>
</template>

<script>
	// https://roundicons.com/icon-packs/free-christmas-icons/
	import tmMenubars from '@/tm-vuetify/components/tm-menubars/tm-menubars.vue';
	import tmSheet from "@/tm-vuetify/components/tm-sheet/tm-sheet.vue"
	import tmAvatar from "@/tm-vuetify/components/tm-avatar/tm-avatar.vue"
	import tmListitem from "@/tm-vuetify/components/tm-listitem/tm-listitem.vue"
	import tmPoup from "@/tm-vuetify/components/tm-poup/tm-poup.vue"
	export default {
		components: {
			tmMenubars,
			tmSheet,
			tmAvatar,
			tmListitem,
			tmPoup
		},
		data() {
			return {
				avatrUrl: 'https://picsum.photos/200',
				userInfo: {},
				share: false
			};
		},
		created() {
			this.sys = uni.getSystemInfoSync();
			// #ifdef H5
			this.bottom = 55
			// #endif

		},
		onLoad() {
			// #ifdef MP
			this.top = uni.upx2px(150);
			// #endif

		},
		mounted() {
			this.judgeLogin((res) => {
				this.userInfo = res
			});
		},
		methods: {
			loadType() {
				this.$ajax.get('/type/load/' + this.er).then(res => {
					this.types = res.data
				})
			},
			selectType(e) {
				this.type = e.data
			},
			closeKey() {
				this.show = false;
			},
			open() {
				this.show = true;
			},
			selectTime(e) {
				this.time = e.year + "/" + e.month + "/" + e.day
			},
			getCurrentTime() {
				//获取当前时间并打印
				let yy = new Date().getFullYear();
				let mm = new Date().getMonth() + 1;
				let dd = new Date().getDate();
				let hh = new Date().getHours();
				let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
				let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
				this.time = yy + '/' + mm + '/' + dd;
			},
			change(e) {
				this.word = ''
				this.type = {}
				this.er = e
				this.loadType()
			},
			openUrl(url) {
				let t = this;
				uni.navigateTo({
					url: url,
					fail: e => {}
				});
			},
			openNotice() {
				let that = this;
				this.judgeLogin((ress) => {
					that.$ajax.post('/user/checkNotice', {
						id: ress.id
					}).then(res => {
						if (res.code == 1) {
							if (!res.data) {
								uni.requestSubscribeMessage({
									tmplIds: ['8PWKNoDfNqGCTARYzKVjhtO3lLaT5nXjIBblyf2BG90'],
									complete: (res) => {
										if (res.errMsg == "requestSubscribeMessage:ok") {
											let dataStr = JSON.stringify(res);
											var reg = RegExp(/accept/);
											if (reg.exec(dataStr)) {
												//包含；
												that.$ajax.post('/user/openNotice', {
													id: ress.id
												}).then(res => {
													if (res.code == 1) {
														uni.$tm.toast('订阅开通成功');
													}
												})
											}
										} else {
											console.log('失败');
										}
									}
								});
							} else {
								uni.showModal({
									title: '提示',
									content: '确定取消订阅',
									success: function(res) {
										if (res.confirm) {
											//包含；
											that.$ajax.post('/user/openNotice', {
												id: ress.id
											}).then(res => {
												if (res.code == 1) {
													uni.$tm.toast('取消订阅成功');
												}
											})
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
							}
						}
					})
				});
			},
			exitLogin() {
				let that = this
				this.judgeLogin((ress) => {
					uni.showModal({
						title: '提示',
						content: '确定退出登录',
						success: function(res) {
							if (res.confirm) {
								that.$ajax.post('/user/logout', {
									id: ress.id
								}).then(res => {})
								uni.removeStorageSync('userInfo');
								uni.switchTab({
									url: '/pages/index/index'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				})
			},
			abort() {
				this.share = true
			},
			confirm(val) {
				this.show = true;
				this.judgeLogin(() => {
					this.$ajax.post('/bill/save', {
						type: this.type.id,
						er: this.er,
						value: val,
						uid: this.$store.state.userInfo.id,
						time: this.time
					}).then(res => {
						this.word = ''
						this.type = {}
					})
				});

			}
		}
	};
</script>

<style lang="scss">
	page,
	body {
		min-height: 100%;
		background-color: #f5f5f5;
	}
</style>
<style lang="scss" scoped></style>
